﻿#include "MaterialDialogFiber.h"
#include "ui_MaterialDialogFiber.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFiber.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QMessageBox>

namespace GUI
{

    MaterialDialogFiber::MaterialDialogFiber(PhysicsDlgExecType type, int matid /*= -1*/, QWidget* parent /*= nullptr*/)
        :AbstractMaterialDialog(type,  parent)
    {
        _ui = new Ui::MaterialDialogFiber;
        _ui->setupUi(this);
        //获取材料对象
        if (_matMgr && matid > 0)
            _mat = dynamic_cast<Acoustics::FITKAcousticsMaterialsFiber*>(_matMgr->getDataByID(matid));

        this->init();

    }

    MaterialDialogFiber::~MaterialDialogFiber()
    {
        if (_ui) delete _ui;
    }
    void MaterialDialogFiber::on_pushButtonOK_clicked()
    {
        if (!this->checkInput()) return;
        //要处理的对象
        Acoustics::FITKAcousticsMaterialsFiber* fiber = dynamic_cast<Acoustics::FITKAcousticsMaterialsFiber*>(_mat);
        //保存执行的脚本
        QStringList str;
        QString name = _ui->lineEdit_Name->text();
        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            str.append("m=Material.Fibers()");
            str.append(QString("m.createMaterial('%1')").arg(name));

            fiber = new Acoustics::FITKAcousticsMaterialsFiber;
            _matMgr->appendDataObj(fiber);
        }
        if (!fiber)return;

        //保存数据
        double density = _ui->lineEdit_Density->text().toDouble();
        double flowResistivity = _ui->lineEdit_FlowResistivity->text().toDouble();
        double tortuosity = _ui->lineEdit_Tortuosity->text().toDouble();
        double porosity = _ui->lineEdit_Porosity->text().toDouble();
        double viscousLength = _ui->lineEdit_Viscous->text().toDouble();
        double thermalLength = _ui->lineEdit_Thermal->text().toDouble();

        if (_execType == GUI::PhysicsDlgExecType::Edit)
        {
            str.append(QString("m=Material.Fibers.GetFibers('%1')").arg(fiber->getDataObjectName()));
            str.append(QString("m.setName('%1')").arg(name));
        }
        str.append(QString("m.setDensityResistivity(%1,%2)").arg(density).arg(flowResistivity));
        str.append(QString("m.setRigidLimpFrame(%1,%2,%3,%4)").arg(porosity).arg(tortuosity).arg(viscousLength).arg(thermalLength));

        fiber->setDataObjectName(name);
        fiber->setDensity(density);
        fiber->setFlowResistivity(flowResistivity);
        fiber->setPorosity(porosity);
        fiber->setTortuosity(tortuosity);
        fiber->setThermalLength(thermalLength);
        fiber->setViscousLength(viscousLength);

        this->saveScript(str);
        this->accept();
    }
    void MaterialDialogFiber::on_pushButtonCancel_clicked()
    {
        this->reject();
    }
    void MaterialDialogFiber::init()
    {
        if (_matMgr == nullptr) return;

        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = _matMgr->checkName("New Fiber Material");
            _ui->lineEdit_Name->setText(name);
        }
        Acoustics::FITKAcousticsMaterialsFiber* fiber = dynamic_cast<Acoustics::FITKAcousticsMaterialsFiber*>(_mat);
        if (!fiber)return;

        // 设置各个属性的值
        _ui->lineEdit_Name->setText(fiber->getDataObjectName());
        _ui->lineEdit_Density->setText(QString::number(fiber->getDensity()));
        _ui->lineEdit_FlowResistivity->setText(QString::number(fiber->getFlowResistivity()));
        _ui->lineEdit_Tortuosity->setText(QString::number(fiber->getTortuosity()));
        _ui->lineEdit_Porosity->setText(QString::number(fiber->getPorosity()));
        _ui->lineEdit_Thermal->setText(QString::number(fiber->getThermalLength()));
        _ui->lineEdit_Viscous->setText(QString::number(fiber->getViscousLength()));

        //复制,更新名称
        if (_execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(fiber->getDataObjectName());
            _ui->lineEdit_Name->setText(text);
        }
    }

    bool MaterialDialogFiber::checkInput()
    {
        //检查数据合法性
        QList<QLineEdit*> checkUi;
        checkUi << _ui->lineEdit_Density << _ui->lineEdit_FlowResistivity << _ui->lineEdit_Tortuosity
            << _ui->lineEdit_Porosity << _ui->lineEdit_Thermal << _ui->lineEdit_Viscous;
        for (QLineEdit* e : checkUi)
        {
            if (e == nullptr) continue;
            QString text = e->text().trimmed();
            bool ok = false;
            text.toDouble(&ok);
            if (!ok)
            {
                QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
                return false;
            }
        }
        if (_matMgr == nullptr) return false;
        //检查名称
        QString text = _ui->lineEdit_Name->text().trimmed();
        if (text != "") {
            QString newText = _matMgr->checkName(text);
            if (text == newText) return true;
            //与自己重名
            if (_matMgr->getDataByName(text) == _mat) return true;
        }

        QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
        return false;
    }
}